﻿using DataBaseDAO;
using Microsoft.AspNetCore.Mvc;
using MyDatabase;

namespace MyCSharpMVCProject.Controllers
{
    public class GoodController : BaseController
    {
        // 调用商品的DAO层对数据库商品表进行操作
        private GoodDAO goodDAO = new GoodDAO();

        public ActionResult GoodsList()
        {
            return View();
        }

        public ActionResult GoodsDetail()
        {
            return View();
        }
        // 更新商品信息
        public ActionResult UpdateSingleGood(Good goods)
        {
            goodDAO.UpdateGood(goods);
            return SuccessResult("修改成功");
        }

        // 根据商品id获取商品信息
        public string GetSingleGood(int Id)
        {
            var data = goodDAO.GetGoodById(Id);
            return data.ToJsonCon();
        }

        // 插入商品
        public ActionResult InsertSingleGood(Good goods)
        {
            goodDAO.InsertGood(goods);
            return SuccessResult("新增成功");
        }
        // 获取商品列表
        public string GetGoodsList(string keyWord)
        {
            var data = goodDAO.GetGoodsList(keyWord);
            return data.ToJsonCon();
        }

        public string GetGoodsListByType(string keyWord)
        {
            var data = goodDAO.GetGoodsListByType(keyWord);
            return data.ToJsonCon();
        }

        public ActionResult DelGoods(int Id)
        {
            bool b = goodDAO.DelGood(Id);
            if (b)
            {
                return SuccessResult("删除成功");
            }
            else
            {
                return ErrorResult("删除失败");
            }
        }
    }
}
